
// 创建一个SegmentView实例
var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var rootview = ui("$");
var openHelper = require("helper/open");
var mallUrl = require("url/mall");
var qqHttp = require("helper/http");
var pubUrl = require("url/pub");
var bizzType = require("helper/bizzType");
var headerRight = ui("headerRight");
var promotionHelper = require("helper/promotion");
var userUtil = require("helper/user");
var selectSpec = {};

page.on("back", function() {
	app.closePage({"product_detail":true});
})
var headerLeft = ui("headerLeft");
headerLeft.on("touch", function() {
	app.closePage({"product_detail":true});
})
var cartNumLayout = ui("cartNumLayout");
var cartNum = ui("cartNum");
var feedbackCountLayout = ui("feedbackCountLayout");
var feedbackCount = ui("feedbackCount");
var addCartBtn = ui("addCartBtn");
var toOrderBtn = ui("toOrderBtn");
//var commentIndex = ui("do_ALayout_comment");
var cartIndex = ui("do_ALayout_cart");

var mallProduct;
var isConcern = 2;
var isCreateCircle=false;
var productId,shopIdData;
var stock = 0;


//接受地图页面关闭的时候传递回来的数据
page.on("result", function(data) {
	if(data && data.cart_main){
		refreshData();
	}
});

//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	var data = page.getData();
	productId=data.id;
	refreshData()
});
var style;
function refreshData(){
	//读取当前页面的传入参数
	qqHttp.post(mallUrl.url.product.detail, {id:productId}, function(data) {
		mallProduct = data.mallProduct;
		if(null!=mallProduct.mallShopVo){
			style=mallProduct.mallShopVo.style;
		}
		if(mallProduct && mallProduct.productSpecLst && mallProduct.productSpecLst.length > 0){
			shopIdData = mallProduct.shopId;
			stock =  mallProduct.productSpecLst[0].stock;
			if(mallProduct.productSpecLst.length > 1){//多规格商品 默认选择第一个
				selectSpec.specId = mallProduct.productSpecLst[0].id;
				selectSpec.specText = mallProduct.productSpecLst[0].color + mallProduct.productSpecLst[0].specName;
				selectSpec.quantity = 1;
				selectSpec.price = mallProduct.productSpecLst[0].price;
				selectSpec.markPrice = mallProduct.productSpecLst[0].marketPrice;
				selectSpec.img = mallProduct.productSpecLst[0].specLogo;
			}
			if(stock < 1){
				toOrderBtn.bgColor = "949EAAFF";
				toOrderBtn.text = "补货中"
				toOrderBtn.enabled = false;
			}
		}
		cartNum.text = data.cartTotalProduct;
		if(cartNum.text > 0) cartNumLayout.visible = true;
		feedbackCount.text = "("+data.feedbackCount+")";
		if(data.feedbackCount > 0) feedbackCountLayout.visible = true;
		isConcern = mallProduct.isConcern;
		if(data.myCreateCircles){
			isCreateCircle=true;
		}else{
			isCreateCircle=false;
		}
		if(isConcern == 1){
			ui("imgConcern").source = "source://image/community/star_on.png";
			ui("labelConcern").text = "取消收藏";
		}else{
			ui("imgConcern").source = "source://image/community/star.png";
			ui("labelConcern").text = "收藏";
		}
		page.fire("productDetailBox",mallProduct);
	});
}

var productShower = ui("productShower");
var showerData = [ {
        "id" : "view0",
        "path" : "source://view/mall/product/detail.ui"
}, {
        "id" : "view1",
        "path" : "source://view/mall/product/content.ui"
}, {
        "id" : "view2",
        "path" : "source://view/mall/comment/list.ui"
}];

// 为viewshower增加4个页面
productShower.addViews(showerData);
productShower.on("viewChanged", function(data, e) {
	if(data=="view1"){
		page.fire("productContent",productId);
	}
	else if(data=="view2"){
		page.fire("productComment",{"productId":productId,"shopId":shopIdData});
	}
});
var headerMiddle = ui("headerMiddle");
var tabtn1 = ui("do_Button_1"),tabtn2 = ui("do_Button_2"), tabtn3 = ui("do_Button_3");
var btnList = [tabtn1,tabtn2,tabtn3];
var btnmove = ui("tabmove");

var paytouch = function(index){
	for(var i=0;i<btnList.length;i++){
		if(i!=index){
			btnList[i].fontColor = "949EAAFF";
		}else{
			btnList[i].fontColor = "FF7E00FF";
			var animtabyes= mm("do_Animator");
			var propstabyes = {x:130*i+30};
			animtabyes.append(300, propstabyes, "EaseOut");
			btnmove.animate(animtabyes);
			headerMiddle.tag = btnList[i].tag;
		}
	}
	productShower.showView("view"+headerMiddle.tag);
};
btnList.forEach(function(dc,i){
	dc.on("touch","",300, function(datac, e) {
		paytouch(i);
	});
});
paytouch(0);
/**
 * 加入购物车
 */
addCartBtn.on("touch",function(){
	if(!userUtil.loginToken()){
		openHelper.openLogin();	
	}else{
		if (mallProduct && mallProduct.status == 1) {
			if(mallProduct.productSpecLst.length > 1 && !selectSpec.specId){
				nf.toast("请选择商品规格");
			}else{
				if(stock < 1){
					nf.toast("商品补货中...");
					return;
				}
				var productSpecId = mallProduct.productSpecLst[0].id;//如果不选择规格和数量则给予默认规格和数量
				var quantity = 1;
				if(mallProduct.productSpecLst.length > 1){
					productSpecId = selectSpec.specId;
					quantity = selectSpec.quantity;
				}
				qqHttp.post(mallUrl.url.cart.doAdd, {productId:mallProduct.id,shopId:mallProduct.shopId, productSpecId:productSpecId,quantity:quantity}, function(data) {
					cartNum.text = data.cartTotalProduct;
					if(cartNum.text > 0) cartNumLayout.visible = true;
					nf.toast("已成功加入购物车");
				});
			}
		}
	}
	
});

/**
 * 立即购买
 */
toOrderBtn.on("touch","","3000",function(){
	if(!userUtil.loginToken()){
		openHelper.openLogin();	
	}else{
		if (mallProduct && mallProduct.status == 1 && mallProduct.stock > 0) {
			if(mallProduct.productSpecLst.length > 1 && !selectSpec.specId){
				nf.toast("请选择商品规格");
			}else{
				if(stock < 1){
					nf.toast("商品补货中...");
					return;
				}
				var total = mallProduct.price;
				var addItems = [];
				var item = {};
				if(mallProduct.productSpecLst.length > 1){
					item.name = mallProduct.name;
					item.productId = selectSpec.specId;
					item.spec = selectSpec.specText;
					item.quantity = selectSpec.quantity;
					item.onlyProductId = mallProduct.id;//id
					item.price = selectSpec.price;
					item.marketPrice =selectSpec.marketPrice;
					item.img = selectSpec.img;
					total = selectSpec.price * selectSpec.quantity;
				}else{
					item.name = mallProduct.name;
					item.productId = mallProduct.productSpecLst[0].id;//默认规格
					item.quantity = 1;
					item.onlyProductId = mallProduct.id;//id
					item.price = mallProduct.productSpecLst[0].price;
					item.marketPrice =mallProduct.productSpecLst[0].marketPrice;
					item.img = mallProduct.firstImg
					total = mallProduct.productSpecLst[0].price;
				}
				item.shopId = mallProduct.shopId;
				item.shopType = mallProduct.shopType;
				if(mallProduct.shopType == 1){//商城
					item.supplierId = mallProduct.supplierId;//商品供应商
					item.supplierType = mallProduct.supplierTypeCode;//商品供应商类型
					item.supplierTypeStr = mallProduct.supplierTypeStr;;//商品供应商类型名称
					addItems.push(item);
					openHelper.open("source://view/mall/cart/toOrder.ui",{addItems:addItems,total:total});
				}else{//圈商
					addItems.push(item);
					openHelper.open("source://view/qshop/cart/toOrder.ui",{addItems:addItems,total:total,style:style,shopId:mallProduct.shopId});
				}	
			}
		}
	}
	
});

/**
 * 购物车按钮
 */
cartIndex.on("touch",function(){
	if(!userUtil.loginToken()){
		openHelper.openLogin();	
	}else{
        if(mallProduct && mallProduct.shopId && mallProduct.shopType){
    		if(mallProduct.shopType == 2){
    			openHelper.open("source://view/qshop/cart/main.ui", {shopId:mallProduct.shopId,style:style});
    		}else if (mallProduct.shopType == 1){
    			openHelper.open("source://view/mall/cart/main.ui", {shopId:mallProduct.shopId,style:style});
    		}
		}
	}
});


/*
 * 菜单弹出
 */
var menus;
var popmenu = rootview.add("menupop", "source://view/reuse/menu/menu.ui", 0, 0);
var popmenuf = ui("menupop");
headerRight.on("touch", function() {
	if(!userUtil.loginToken()){
		openHelper.openLogin();	
	}else{
		/*if(isCreateCircle){
			menus=[{
				"template" : 0,
				"menuIcon":"source://image/menu/order.png",
				"menuName":"我的订单",
				"type" : "myOrders"
				},{
				  "template" : 0,
				  "menuIcon":"source://image/promotion.png",
				  "menuName":"推广到圈子",
				  "type" : "addPromotion"
				}];
		}else{*/
			menus=[{
				"template" : 0,
				"menuIcon":"source://image/menu/order.png",
				"menuName":"我的订单"
				}];
		//}
		
		popmenuf.fire("popmenuSJ", menus);
	}
});

popmenuf.on("popmenuItem", function(data) {
	popmenuf.fire("popmenuClose");// 关闭菜单
	//var touchIndex = parseInt(data);
	//var actionType = menus[touchIndex].type;

		//if (actionType == "myOrders") {// 订单
			openHelper.open("source://view/order/list.ui");
		//}
	
});

page.on("result", function(data) {
	var circleIds=data.circleIds;
	 if(data.type=="circle"){
		 nf.confirm("要推广到圈子吗？", "推广", "确定", "取消", function(data, e) {
			if (data == 1) {
				promotionMallProduct(circleIds);
			}
		})
	}
});
/*function promotionMallProduct(circleIds){
	var imgs=mallProduct.img.split(";");
	qqHttp.post(pubUrl.url.promotion.create,{img:imgs[0],title:mallProduct.name,sourceBizzId:mallProduct.id,sourceBizzType:promotionHelper.sourceBizzType.MALL_PRODUCT,targetBizzType:promotionHelper.targetBizzType.CIRCLE,circleIds:circleIds},function(data){
		nf.toast("推广成功");
	});
}*/


//收藏按钮
ui("do_ALayout_concern").on("touch", function(data, e) {
	if(!userUtil.loginToken()){
		openHelper.openLogin();	
	}else{
		var productId = mallProduct.id;
		var addParams = {
	            bizzId:productId,
	            bizzType:bizzType.Level2.MALL_PRODUCT
			};
		if(isConcern == 1){
			//如果已收藏，再次点击则取消收藏
			qqHttp.post(pubUrl.url.concern.doCancle,addParams,function(data){
				isConcern = 2;
				ui("imgConcern").source = "source://image/community/star.png";
				ui("labelConcern").text = "收藏";
				nf.toast("取消收藏");
			});
		}else{
			qqHttp.post(pubUrl.url.concern.doAdd,addParams,function(data){
				isConcern = 1;
				ui("imgConcern").source = "source://image/community/star_on.png";
				ui("labelConcern").text = "取消收藏";
				nf.toast("已收藏");
			});
		}
	}
});

var addPicker = rootview.add("picker","source://view/mall/product/specSelect.ui", 0, 0);
var picker = ui("picker");
var specList = ui(addPicker+".specList");
specList.on("touch", function(index) {
	page.fire("selectSpec",index);	
});


//选择规格后返回刷新
page.on("refreshSpec",function(data){
	selectSpec.specId = data.specId;
	selectSpec.quantity = data.quantity;
	selectSpec.price = data.price;
	selectSpec.markPrice = data.marketPrice;
	selectSpec.img = data.img;
	selectSpec.specText = data.specText;
	stock =  data.quantity;
	if(stock > 0){
		toOrderBtn.bgColor = "FF7F00FF";
		toOrderBtn.text = "立即购买"
		toOrderBtn.enabled = true;
	}
});

var addShare = rootview.add("share","source://view/reuse/share.ui", 0, 0);